const registerForm = document.querySelector('.register-form');//注册表单
registerForm.addEventListener('submit', function (e) {
    //阻止默认事件
    e.preventDefault()
    const data = serialize(registerForm, { hash: true, empty: true })
    // console.log(data);
    //用户名校验: 2-30个字符
    if (!/[a-zA-Z0-9]{2,30}/.test(data.username)) {
        toastTop('用户名不规范,请输入2-30个字符')
        return
    }
    //密码校验: 6-30个字符
    if (!/[a-zA-Z0-9]{6,30}/.test(data.password)) {
        toastTop('密码不规范,请输入6-30个字符')
        return
    }
    //
    axios({
        method: 'post',
        url: '/register',
        data
    }).then((result) => {
        // console.log(result);
        toastTop(result.message)
        setTimeout(() => {
            location.href = 'login.html'
        }, 1500)
    }).catch(error => {
        // console.dir(error)
        toastTop(error.response.data.message)
    })


})
